package com.ruitu.arad.api.okhttp.callback;

import android.content.Context;

import com.ruitu.arad.api.okhttp.MyOkHttp;
import com.ruitu.arad.api.okhttp.PrefUtil;
import com.ruitu.arad.api.okhttp.Url;
import com.ruitu.arad.api.okhttp.response.IResponseHandler;
import com.ruitu.arad.api.okhttp.util.LogUtils;
import com.ruitu.arad.util.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
import okhttp3.ResponseBody;

// Created by tsy on 16/9/18.
public class MyCallback implements Callback {
    private IResponseHandler mResponseHandler;
    private Context mContext;

    public MyCallback(Context context, IResponseHandler responseHandler) {
        mResponseHandler = responseHandler;
        mContext = context;
    }

    @Override
    public void onFailure(Call call, final IOException e) {
        LogUtils.e("onFailure", e);
        MyOkHttp.mHandler.post(new Runnable() {
            @Override
            public void run() {
                mResponseHandler.onFailure(0, e.toString());
            }
        });
    }

    @Override
    public void onResponse(Call call, final Response response) throws IOException {
//        if (response.isSuccessful()) {
        // 顺和国际项目 401:未登录
        if (response.isSuccessful() || response.code() == 401) {
            mResponseHandler.onSuccess(response);
        } else {
            LogUtils.e("onResponse fail status = " + response.code() + " msg = " + response.message());
            ResponseBody body = response.body();
            final String tempResponse = response.body().string();
            MyOkHttp.mHandler.post(new Runnable() {
                @Override
                public void run() {
                    mResponseHandler.onFailure(response.code(), "" + response.message());
                    switch (response.code()) {
                        case 401:
                        case 407: {
                            PrefUtil.put(mContext, PrefUtil.LOGIN_TOKEN, "");
                            break;
                        }
                        case 402: {
                            break;
                        }
                        default: {
                            try {
                                JSONObject jsonObject = new JSONObject(tempResponse);
                                if ("false".equals(jsonObject.optString("success"))) {
                                    String url = Url.CHECK_UPDATE;
                                    String uri = response.request().url().toString();
                                    if (uri.indexOf("?") != -1) {
                                        uri = uri.substring(0, uri.indexOf("?"));
                                    } else {
                                        String message = jsonObject.optString("message");
                                        if (message.length() > 0) {
                                            ToastUtils.showShort(message);
                                        }
                                    }
                                } else {

                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            break;
                        }
                    }
                }
            });
        }
    }
}